package com.meixun.xuanke.course.controller;

import com.alibaba.fastjson.JSONObject;
import com.meixun.xuanke.course.model.Course;
import com.meixun.xuanke.course.model.User;
import com.meixun.xuanke.course.service.CourseService;
import com.meixun.xuanke.course.util.ParamUtil;
import com.meixun.xuanke.course.util.RequestUtil;
import com.meixun.xuanke.course.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@RestController
public class CourseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CourseService courseService;

    /**
     * 查询学校的选课配置
     * @return
     */
    @ResponseBody
    @RequestMapping("/getConfig")
    public String getConfig (HttpServletRequest request, String schoolId){
        schoolId=ParamUtil.isNull(schoolId)?RequestUtil.getSchoolId(request):schoolId;
        if (ParamUtil.isNull(schoolId)){
            return ResultUtil.getErrorjson("参数错误");
        }
        Integer termTypeid=courseService.queryTermOne();
        JSONObject config = courseService.getConfig(schoolId,termTypeid);
        if (config!=null){
            return ResultUtil.getjson(config,"");
        }
        return ResultUtil.getEmptyjson("没有查询到数据");
    }

    /**
     * 查询选课相关数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCourseData")
    public String getCourseData (HttpServletRequest request){
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        HashMap<String, Object> data = new HashMap<>();
        Integer termTypeid=courseService.queryTermOne();
        JSONObject configJson = courseService.getConfig(user.getSchoolId(),termTypeid);
        Object subTypeList=courseService.querySubType(user.getSchoolId());
        Object courseList=courseService.queryCourseListByStu(user);
        int myCourseCount=courseService.getMyCourseSize(user);
        //当前时间
        data.put("nowTime",new Date());
        //选课配置
        data.put("configJson",configJson);
        //分类集合
        data.put("subTypeList",subTypeList);
        //课程集合
        data.put("courseList",courseList);
        //我的课程数量
        data.put("myCourseCount",myCourseCount);

        return ResultUtil.getjson(data,"");
    }
    /**
     * 查询选课相关数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getMyCourseList")
    public String getMyCourseList (HttpServletRequest request){
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        List<Course> courseList=courseService.getMyCourseList(user);
        return ResultUtil.getjson(courseList,"");
    }

    /**
     * 查询选课详情数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCourseDetail")
    public String getCourseDetail (HttpServletRequest request,Integer id){
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        HashMap<String, Object> data = new HashMap<>();
        Integer termTypeid=courseService.queryTermOne();
        JSONObject configJson = courseService.getConfig(user.getSchoolId(),termTypeid);
        Course course=courseService.getCourseDetail(id,user);
        //当前时间
        data.put("nowTime",new Date());
        //选课配置
        data.put("configJson",configJson);
        //课程详情
        data.put("course",course);
        return ResultUtil.getjson(data,"");
    }

    /**
     * 选课
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addCourse")
    public String addCourse (HttpServletRequest request,Integer id){
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        courseService.addCourse(id,user);
        return ResultUtil.getjson("","");
    }

    /**
     * 获取选课结果
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCourseResult")
    public String getCourseResult (HttpServletRequest request,Integer id,Integer type){
        if(type==null){
            //选课
            type=0;
        }
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        courseService.getCourseResult(id,user,type);
        return ResultUtil.getjson("","");
    }
    /**
     * 退课
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/removeCourse")
    public String removeCourse (HttpServletRequest request,Integer id){
        RequestUtil.checkLogin(request);
        User user =RequestUtil.getUser(request);
        courseService.removeCourse(id,user);
        return ResultUtil.getjson("","");
    }
}
